// custom-tab-bar/index.js
Component({
  data: {
    selected: 0,
    color: "#666666",
    selectedColor: "#ff6b35",
    list: [
      {
        pagePath: "/pages/index/index",
        text: "首页",
        iconClass: "home"
      },
      {
        pagePath: "/pages/menu/menu",
        text: "菜单",
        iconClass: "menu"
      },
      {
        pagePath: "/pages/cart/cart",
        text: "购物车",
        iconClass: "cart"
      },
      {
        pagePath: "/pages/orders/orders",
        text: "订单",
        iconClass: "order"
      },
      {
        pagePath: "/pages/profile/profile",
        text: "我的",
        iconClass: "profile"
      }
    ]
  },
  
  attached() {
    // 获取当前页面路径，设置选中状态
    const pages = getCurrentPages()
    const currentPage = pages[pages.length - 1]
    const currentRoute = currentPage.route
    
    const selected = this.data.list.findIndex(item => 
      item.pagePath === `/${currentRoute}`
    )
    
    if (selected !== -1) {
      this.setData({
        selected: selected
      })
    }
  },
  
  methods: {
    switchTab(e) {
      const data = e.currentTarget.dataset
      const url = data.path
      
      wx.switchTab({
        url: url
      })
      
      this.setData({
        selected: data.index
      })
    }
  }
})